package com.mgq.netty.c6;

import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import io.netty.util.concurrent.Promise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;

@Slf4j
public class TestNettyPromise {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        EventLoopGroup group = new NioEventLoopGroup();
        EventLoop eventLoop = group.next();

        //创建一个promise,前面future的结果不是由我们设置的,为了更灵活,我们引入了promise,我们可以设置任务执行的结果
        Promise<Integer> promise = new DefaultPromise<>(eventLoop);
        new Thread(()->{
            log.debug("开始执行计算");
            try {
                Thread.sleep(1000);
                int i=1/0;
                promise.setSuccess(80);
            } catch (Exception e) {
                promise.setFailure(e);
            }
        }).start();
        log.debug("准备获取结果");
        log.debug("结果是:{}",promise.get());
    }
}
